package org.awenlice.playerdailyshop.data;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.saveddata.SavedData;

import java.util.ArrayList;
import java.util.List;

public class PlayerBuyShopData extends SavedData {
    private List<ItemStack> buyShopItems = new ArrayList<>();

    public List<ItemStack> getBuyShopItems() {
        return buyShopItems;
    }

    public void setBuyShopItems(List<ItemStack> buyShopItems) {
        this.buyShopItems = buyShopItems;
        setDirty(); // 标记数据为脏，需要保存
    }

    @Override
    public CompoundTag save(CompoundTag tag) {
        // 将收购商店数据保存到 NBT
        CompoundTag itemsTag = new CompoundTag();
        for (int i = 0; i < buyShopItems.size(); i++) {
            itemsTag.put("item" + i, buyShopItems.get(i).save(new CompoundTag()));
        }
        tag.put("buyShopItems", itemsTag);
        return tag;
    }

    public static PlayerBuyShopData load(CompoundTag tag) {
        PlayerBuyShopData data = new PlayerBuyShopData();
        CompoundTag itemsTag = tag.getCompound("buyShopItems");
        for (int i = 0; i < 27; i++) { // 假设商店有 27 个槽位
            if (itemsTag.contains("item" + i)) {
                data.buyShopItems.add(ItemStack.of(itemsTag.getCompound("item" + i)));
            }
        }
        return data;
    }
}